using MediatR;
using Microsoft.AspNetCore.Mvc;
using SupplierCustomerManagement.Write.API.Applications.Commands.servicepersonals;
using B.S.RbacData.ErrorCode;

namespace SupplierCustomerManagement.Write.API.Controllers
{
    /// <summary>
    /// 服务人员管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ServicePersonalsController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<ServicePersonalsController> _logger;

        public ServicePersonalsController(IMediator mediator, ILogger<ServicePersonalsController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 新增服务人员
        /// </summary>
        /// <param name="command">新增服务人员命令</param>
        /// <returns>新增结果</returns>
        [HttpPost]
        public async Task<ActionResult<ApiResult<int>>> AddServicePersonal([FromBody] AddServicePersonalCommand command)
        {
            try
            {
                var result = await _mediator.Send(command);

                _logger.LogInformation("成功处理新增服务人员请求，关联实体ID: {RelatedEntityId}, 服务人员姓名: {Name}", 
                    command.RelatedEntityId, command.Name);

                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "处理新增服务人员请求时发生异常");
                
                var errorResult = new ApiResult<int>
                {
                    code = ResultEnumCode.fails,
                    message = $"新增服务人员失败: {ex.Message}",
                    data = 0
                };

                return StatusCode(500, errorResult);
            }
        }

        /// <summary>
        /// 更新服务人员
        /// </summary>
        /// <param name="command">更新服务人员命令</param>
        /// <returns>更新结果</returns>
        [HttpPost]
        public async Task<ActionResult<ApiResult<bool>>> UpdateServicePersonal([FromBody] UpdateServicePersonalCommand command)
        {
            try
            {
                var result = await _mediator.Send(command);

                _logger.LogInformation("成功处理更新服务人员请求，服务人员ID: {ServicePersonalId}", command.Id);

                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "处理更新服务人员请求时发生异常，服务人员ID: {ServicePersonalId}", command.Id);
                
                var errorResult = new ApiResult<bool>
                {
                    code = ResultEnumCode.fails,
                    message = $"更新服务人员失败: {ex.Message}",
                    data = false
                };

                return StatusCode(500, errorResult);
            }
        }

        /// <summary>
        /// 删除服务人员
        /// </summary>
        /// <param name="command">删除服务人员命令</param>
        /// <returns>删除结果</returns>
        [HttpPost]
        public async Task<ActionResult<ApiResult<bool>>> DeleteServicePersonal([FromBody] DeleteServicePersonalCommand command)
        {
            try
            {
                var result = await _mediator.Send(command);

                _logger.LogInformation("成功处理删除服务人员请求，服务人员ID: {ServicePersonalId}", command.Id);

                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "处理删除服务人员请求时发生异常，服务人员ID: {ServicePersonalId}", command.Id);
                
                var errorResult = new ApiResult<bool>
                {
                    code = ResultEnumCode.fails,
                    message = $"删除服务人员失败: {ex.Message}",
                    data = false
                };

                return StatusCode(500, errorResult);
            }
        }
    }
}